package jp.ac.aiit.yc.sww.database.dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static jp.ac.aiit.yc.sww.constant.Constants.*;
import static jp.ac.aiit.yc.sww.util.Util.*;
import jp.ac.aiit.yc.sww.database.dto.DrawDto;
import jp.ac.aiit.yc.sww.database.dto.PresentDto;
import jp.ac.aiit.yc.sww.database.dto.SwwDtoIF;
import jp.ac.aiit.yc.sww.Resources.*;


public class DrawDao extends BaseDAO implements SwwDaoIF
{

	public String getInsertSP() {
		return getSP(SwwResource.SP_DRAW_EVENT);
	}

	public String getQuerySP() {
		return getSP(SwwResource.SP_DRAW_INFO);
	}

	public CallableStatement setInsertParam(CallableStatement cStmt, SwwDtoIF dtoIF) throws Exception{
		DrawDto dto =  (DrawDto)dtoIF;
		Map headers = getHeaders();
		// TODO データのチェックが必要
//		System.out.println("CallableStatement setInsertParam");
//		System.out.println((new Date()).toString() + " headers.get(HEADER_USER)=" + null2String(headers.get(HEADER_USER)));
//		System.out.println((new Date()).toString() + " headers.get(HEADER_ROOM)=" + null2String(headers.get(HEADER_ROOM)));
//		System.out.println((new Date()).toString() + " headers.get(HEADER_PAGE)=" + parseInt(headers.get(HEADER_PAGE)));
//		System.out.println((new Date()).toString() + " dto.getColor()=" + dto.getColor());
//		System.out.println((new Date()).toString() + " dto.getThickness()=" + dto.getThickness());
//		System.out.println((new Date()).toString() + " dto.getPoints()=" + dto.getPoints());

		cStmt.setString(1, null2String(headers.get(HEADER_USER)));
		cStmt.setString(2, null2String(headers.get(HEADER_ROOM)));
		cStmt.setInt(3, parseInt(headers.get(HEADER_PAGE)));
		cStmt.setLong(4, dto.getColor());
		cStmt.setLong(5,dto.getThickness());
		cStmt.setString(6, dto.getPoints());

		/*
		CREATE PROCEDURE sp_draw_event (
				IN var_user_id VARCHAR(10),
				IN var_meeting_room_id VARCHAR(10),
				IN var_page_id INT UNSIGNED,
				IN var_color_id INT UNSIGNED,
				IN var_size INT UNSIGNED,
				IN var_points TEXT)
		*/
//		System.out.println((new Date()).toString() + " return cstmt");
		return cStmt;
	}

	@Override
	public CallableStatement setQueryParam(CallableStatement cstmt, SwwDtoIF dtoIF)
			throws Exception {
		PresentDto dto =  (PresentDto)dtoIF;
//		System.out.println((new Date()).toString() + " dto.getDraw_e_ids=" + dto.getDraw_e_ids());
		cstmt.setInt(1, Integer.parseInt(dto.getDraw_e_ids()));
		return cstmt;
	}


	public boolean register(SwwDtoIF dto) {
		return false;
	}

	@Override
	public Map<String, String> getHeaders() {
		return super.getHeaders();
	}

	@Override
	public void setHeaders(Map<String, String> headers) {
		super.setHeaders(headers);

	}



	@Override
	public SwwDtoIF createDto(ResultSet rs) throws Exception {
		DrawDto dto = new DrawDto();

		// TODO 引数は外部化
		String color = rs.getString("color_id");
		int size = rs.getInt("size");
		float x  = rs.getFloat("x");
		float y  = rs.getFloat("y");

		// TODO Long, int, floatの型変換
		dto.setColor(Long.parseLong(color));
		dto.setThickness(Long.valueOf(size));;
		dto.setPoints(Float.toString(x) + "," + Float.toString(y));
		/*
		 * 	SELECT	draw_event.draw_e_id,
			draw_points.draw_points_id,
			draw_event.color_id,
			draw_event.size,
			draw_points.x,
			draw_points.y
		 *
		 * */
/*
 * 	          int id = rs.getInt("draw_e_id");
	    	  //色を取得
	          String color = rs.getString("color_id");
	          // サイズを取得
	          int size = rs.getInt("size");
	          //ｘ座標を取得
	          float x = rs.getFloat("x");
	          // ｙ座標を取得
	          float y = rs.getFloat("y");
 *
 * */
        return dto;
	}
}
